
function getRandom1(conf, without, weight_index)
    local sum = 0
    for i = 1 , #conf do
        if canCalc(i, without) then
            sum = sum + conf[i][weight_index]
        end
    end

    local r = math.random(1, sum)
    sum = 0
    for i = 1 , #conf do
        if canCalc(i, without) then
            sum = sum + conf[i][weight_index]
            if sum >= r then
                return i
            end
        end
    end
end

function getRandom2(conf, without)
    local sum = 0
    for k, v in ipairs(conf) do
        if canCalc(k, without) then
            sum = sum + v.weight
        end
    end
    local r = math.random(1, sum)
    sum = 0
    for k, v in ipairs(conf) do
        if canCalc(k, without) then
            sum = sum + v.weight
            if sum >= r then
                return k
            end
        end
    end
end

function getRandom3(conf)
    local sum = 0
    for k, v in ipairs(conf) do
        sum = sum + v.weight
    end
    local r = math.random(1, sum)
    sum = 0
    for k, v in ipairs(conf) do
        sum = sum + v.weight
        if sum >= r then
            return k
        end
    end
end


function getRandom4(conf, without, weight_index, minLvIndex, maxLvIndex, human)
    local sum = 0
    for i = 1 , #conf do
        if canCalc(i, without) and (not conf[i][minLvIndex] or
            human and human.db.lv >= conf[i][minLvIndex] and human.db.lv <= conf[i][maxLvIndex]) then
            sum = sum + conf[i][weight_index]
        end
    end
    local r = math.random(1, sum)
    sum = 0
    for i = 1 , #conf do
        if canCalc(i, without) and (not conf[i][minLvIndex] or
            human and human.db.lv >= conf[i][minLvIndex] and human.db.lv <= conf[i][maxLvIndex]) then
            sum = sum + conf[i][weight_index]
            if sum >= r then
                return i
            end
        end
    end
end

function getRandom5(human, conf, without, weight_index, shenbing_index, sex_index)
    local sum = 0
    for i = 1 , #conf do
        if canCalc(i, without) then
            local shenbingid = conf[i][shenbing_index]
            local sex = conf[i][sex_index]
            if (shenbingid == 0 or human.db.shenbing[0].id == shenbingid) and (sex == 0 or human.db.sex == sex) then
                sum = sum + conf[i][weight_index]
            end
        end
    end

    if sum == 0 then
        assert()
    end

    local r = math.random(1, sum)
    sum = 0
    for i = 1 , #conf do
        if canCalc(i, without) then
            local shenbingid = conf[i][shenbing_index]
            local sex = conf[i][sex_index]
            if (shenbingid == 0 or human.db.shenbing[0].id == shenbingid) and (sex == 0 or human.db.sex == sex) then
                sum = sum + conf[i][weight_index]
                if sum >= r then
                    return i
                end
            end
        end
    end
end

function canCalc(index, without)
    if not without then
        return true
    end
    if without then
        for k, v in ipairs(without) do
            if index == v then
                return false
            end
        end
    end
    return true
end
